<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/19
 * Time: 下午8:12
 */

class ModGiftBag extends Model {

    protected $table = 'gift_bag';

    // 表名
    public function tableName(){
        return $this->table;
    }

    public static function model($className=__CLASS__){
        return parent::model($className);
    }

    public function getDbConnection(){
        return Yii::app()->dbOss;
    }
    
    public function page($params){
        $where  = ' WHERE platform=:platform AND game=:game ';
        $args   = array(
            ':platform' => 'test',
            ':game'     => $params['g']
        );
        
        if(!empty($params['classify'])){
        	$where .= ' AND classify = :classify ';
        	$args[':classify'] = $params['classify'];
        }
        
        if(!empty($params['bag_name'])){
        	$where .= ' AND bag_name = :bag_name ';
        	$args[':bag_name'] = $params['bag_name'];
        }
        
        
        if($params['begin'] && $params['end']){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] != '' && $params['end'] == ""){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = time();
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] == '' && $params['end'] != ""){
        	$params['begin'] = date('Y-m-01');
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }

    	$c_sql = "select count(1) as cnt from {$this->table} as c {$where}";
    	$c_result = $this->getDbConnection()->query($c_sql, $args);
    	$count = isset($c_result[0]['cnt']) ? $c_result[0]['cnt'] : 0;
    
    	$order  = self::getOrder($params['order']);
    	$limit  = PubFunc::getLimit($params['length'], $params['page']);
    	$fields = "c.*";
    	$sql = "SELECT {$fields} FROM {$this->table} as c {$where} {$order} {$limit}";
    	
    	$data = $this->getDbConnection()->query($sql, $args);
    	$result = array('count'=>$count,'data'=>$data);
    	return $result;
    }
    
    public static function getOrder($order){
    	if(PubFunc::checkEmpty($order, array(0, 1))) return '';
    	if(!in_array($order[1], array('asc', 'desc'))){
    		$sort = 'asc';
    	}else{
    		$sort = $order[1];
    	}
    	return " ORDER BY {$order[0]} {$sort} ";
    }

    public function select($params){
        $where  = ' WHERE platform=:platform AND game=:game ';
        $args   = array(
            ':platform' => 'test',
            ':game'     => $params['g']
        );

        if(!empty($params['classify'])){
        	$where .= ' AND classify = :classify ';
        	$args[':classify'] = $params['classify'];
        }
        
        if(!empty($params['bag_name'])){
        	$where .= ' AND bag_name = :bag_name ';
        	$args[':bag_name'] = $params['bag_name'];
        }
        
        
        if($params['begin'] && $params['end']){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] != '' && $params['end'] == ""){
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = time();
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }
        
        if($params['begin'] == '' && $params['end'] != ""){
        	$params['begin'] = date('Y-m-01');
        	$begin = strtotime($params['begin'] . ' 00:00:00');
        	$end = strtotime($params['end'] . ' 23:59:59');
        	$where  .= " AND create_time BETWEEN ".$begin. " AND  ".$end;
        }

        $sql = "SELECT * FROM {$this->table} {$where} ORDER BY create_time DESC";
       
        $result = $this->getDbConnection()->getAll($sql, $args);
        return $result;
    }
    

    
} 